#include "genomefactory.h"

GenomeFactory::GenomeFactory(QString ptf, QList < QPointer< Contig > >* c, BacterialContigBrowser* pw, QObject *parent) :
    QObject(parent)
{
    pathToFile.append(ptf);
    mutex.lock();
    parentWidget = pw;
    mutex.unlock();

    mutex.lock();
    contigs = c;
    mutex.unlock();
}

void GenomeFactory::process()
{
    bool worked = readReformatedFile();

    emit( gotContigs(contigs) );

    emit(trackProgress(100));
    emit(finished());
}


bool GenomeFactory::readReformatedFile()
{
    //mutex.lock();
    QFile file(pathToFile);

    if( !file.exists() )//make sure file exists
    {
        qDebug() << "file not found." << endl;
        return false;
    }

    if( ! file.open(QIODevice::ReadOnly)) //and can be openned
    {
        qDebug() << "file " << pathToFile << " can't be opened." << endl;
        return false;
    }

    qDebug() << "file " << pathToFile << " opened." << endl;

    QTextStream stream(&file);//text stream to stream data in from file

    QHash< QString, int > contigHash;//this allows me to keep track of whether contig listed on a line has already been encountered
    int contigCount = 0;

    QHash< QString, int > geneHash; //same as for contigs- except I need this to generate a complete gene list
    int geneCount;

    //maxNTPerContig = 0;


    QString line;//get each line
    do {
         line = stream.readLine();

         qDebug() << "reading: " << line << endl;

        //Note: for now I am assuming that each contig is in a sep strain- but this may not always be true
         //will have to adapt

         //first get the contig idx
         int contigIdx = -1;
         QStringList list = line.split("\t");//tab delimited- so returns the contig, gene, start, stop, strand
         QString contigId = list[0].append( "_").append(list[1]).append("_").append( list[2] );
         if( contigHash.contains( contigId )) //if already encountered this contig
         {
             contigIdx = contigHash.value(contigId);//get the idx
         }
         else //not encountered
         {
             contigIdx = contigCount;
             contigCount++;//counter hold the number of contigs that have been added thus far
             mutex.lock();
             contigs->append( new Contig( contigId, parentWidget ) );//, (BacterialContigBrowser) this->parent() );//list[0], this ); //create contig
             //contigs[ contigs.size()-1 ]->setVisible(false);
             mutex.unlock();
             contigHash.insert( contigId, contigIdx );//list[0], contigIdx);//add to hash table
         }

         QString geneId = list[7]; //now getting geneId from 7 - which is really the cluster id.  Will change this later
         int start = list[4].toInt();
         int stop = list[5].toInt();
         bool posStrand = list[6].contains("+") ? true : false;

         mutex.lock();
         contigs->at( contigIdx )->addGene( geneId, start, stop, posStrand, line, "reformatted" );
         mutex.unlock();
         //if( stop > maxNTPerContig )
         //    maxNTPerContig = stop;

    } while (!stream.atEnd() );//&& contigCount <= maxContigsDisplayed );
    //note- currently loading only the data that is displayed.  Eventually this means I will have to implement caching

    qDebug() << "done reading" << endl;
    file.close();

   //mutex.unlock();

    return true;
}



bool GenomeFactory::readReformatedFile2()
{
    //mutex.lock();
    QFile file(pathToFile);

    if( !file.exists() )//make sure file exists
    {
        qDebug() << "file not found." << endl;
        return false;
    }

    if( ! file.open(QIODevice::ReadOnly)) //and can be openned
    {
        qDebug() << "file " << pathToFile << " can't be opened." << endl;
        return false;
    }

    qDebug() << "file " << pathToFile << " opened." << endl;

    QTextStream stream(&file);//text stream to stream data in from file

    QHash< QString, int > contigHash;//this allows me to keep track of whether contig listed on a line has already been encountered
    int contigCount = 0;

    QHash< QString, int > geneHash; //same as for contigs- except I need this to generate a complete gene list
    int geneCount;

    //maxNTPerContig = 0;


    QString line;//get each line
    do {
         line = stream.readLine();

         qDebug() << "reading: " << line << endl;

        //Note: for now I am assuming that each contig is in a sep strain- but this may not always be true
         //will have to adapt

         //first get the contig idx
         int contigIdx = -1;
         QStringList list = line.split("\t");//tab delimited- so returns the contig, gene, start, stop, strand
         QString contigId = list[0].append( "_").append(list[1]).append("_").append( list[2] );
         if( contigHash.contains( contigId )) //if already encountered this contig
         {
             contigIdx = contigHash.value(contigId);//get the idx
         }
         else //not encountered
         {
             contigIdx = contigCount;
             contigCount++;//counter hold the number of contigs that have been added thus far
             mutex.lock();
             contigs->append( new Contig( contigId, parentWidget ) );//, (BacterialContigBrowser) this->parent() );//list[0], this ); //create contig
             //contigs[ contigs.size()-1 ]->setVisible(false);
             mutex.unlock();
             contigHash.insert( contigId, contigIdx );//list[0], contigIdx);//add to hash table
         }

         QString geneId = list[7]; //now getting geneId from 7 - which is really the cluster id.  Will change this later
         int start = list[4].toInt();
         int stop = list[5].toInt();
         bool posStrand = list[6].contains("+") ? true : false;

         mutex.lock();
         contigs->at( contigIdx )->addGene( geneId, start, stop, posStrand, line, "reformatted" );
         mutex.unlock();
         //if( stop > maxNTPerContig )
         //    maxNTPerContig = stop;

    } while (!stream.atEnd() );//&& contigCount <= maxContigsDisplayed );
    //note- currently loading only the data that is displayed.  Eventually this means I will have to implement caching

    qDebug() << "done reading" << endl;
    file.close();

   //mutex.unlock();

    return true;
}

